package com.sky.business.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.business.sys.domain.TrackTask;
import com.sky.business.sys.mapper.TrackTaskMapper;
import com.sky.business.sys.service.TrackTaskService;
import com.sky.common.enums.DispatchType;
import com.sky.common.enums.TrackStatus;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TrackTaskServiceImpl extends ServiceImpl<TrackTaskMapper, TrackTask> implements TrackTaskService {

    @Override
    public  void create(String deviceId, String trackTaskId, DispatchType dispatchType) {
        TrackTask trackTask = new TrackTask();
        trackTask.setTaskId(trackTaskId);
        trackTask.setDeviceId(deviceId);
        trackTask.setStatus(TrackStatus.TRACKING);
        trackTask.setDispatchType(dispatchType);
        trackTask.setBeginTime(new Date());
        save(trackTask);
    }

    @Override
    public List<TrackTask> findNoCmccTaskByTaskIdNotEqualsAndDeviceIdIn(String taskId, List<String> deviceIdList) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(TrackTask::getTaskId,taskId);
        queryWrapper.in(TrackTask::getDeviceId, deviceIdList);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        queryWrapper.ne(TrackTask::getDispatchType, DispatchType.CMCC);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<TrackTask> findByTaskIdAndDeviceId(String taskId, String deviceId) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getTaskId,taskId);
        queryWrapper.eq(TrackTask::getDeviceId, deviceId);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        return getBaseMapper().selectList(queryWrapper);
    }


    @Override
    public List<TrackTask> findByTaskIdAndStatus(String taskId,TrackStatus trackStatus) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getTaskId, taskId);
        queryWrapper.eq(TrackTask::getStatus,trackStatus);
        return getBaseMapper().selectList(queryWrapper);
    }


    @Override
    public void endTrackByTaskIdAndDeviceId(String taskId,String deviceId,Date endTime) {
        LambdaUpdateWrapper<TrackTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(TrackTask::getStatus, TrackStatus.OVER)
                .set(TrackTask::getEndTime,endTime)
                .eq(TrackTask::getDeviceId, deviceId)
                .eq(TrackTask::getTaskId,taskId)
                .eq(TrackTask::getStatus, TrackStatus.TRACKING);
        getBaseMapper().update(updateWrapper);
    }

    @Override
    public void endTrack(TrackTask trackTask,Date endTime) {
        trackTask.setStatus(TrackStatus.OVER);
        trackTask.setEndTime(endTime);
        updateById(trackTask);
    }

    @Override
    public List<TrackTask> findByStatus(TrackStatus trackStatus) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getStatus, trackStatus);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<TrackTask> findByTaskIdAndDeviceIdNotIn(String taskId, List<String> deviceIdList) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getTaskId,taskId);
        queryWrapper.notIn(TrackTask::getDeviceId, deviceIdList);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<TrackTask> findByTaskIdAndDeviceIdIn(String taskId, List<String> deviceIdList) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getTaskId,taskId);
        queryWrapper.in(TrackTask::getDeviceId, deviceIdList);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<TrackTask> findByTaskIdNotEqualsAndDeviceIdIn(String taskId, List<String> deviceIdList) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(TrackTask::getTaskId,taskId);
        queryWrapper.in(TrackTask::getDeviceId, deviceIdList);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<String> getMultiTaskingDevices() {
        return getBaseMapper().getMultiTaskingDevices();
    }

    @Override
    public List<TrackTask> findByDeviceId(String deviceId) {
        LambdaQueryWrapper<TrackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackTask::getDeviceId,deviceId);
        queryWrapper.eq(TrackTask::getStatus, TrackStatus.TRACKING);
        queryWrapper.orderByAsc(TrackTask::getDispatchType);
        return getBaseMapper().selectList(queryWrapper);
    }
}
